﻿using UnityEngine;
using System.Collections;

public class PlayerPlane :Plane
{
	//修饰符     数据类型        变量名
	private       Rigidbody2D      rig; //刚体
	public float speed = 5f;
	public GameObject bulletPrefab;//子弹预制体
	public AudioClip shootsood;
	private AudioSource audioSouce;
	public float shootlnterval=0.15f;
	protected override void Start()//开始的时候执行1次
	{
		base.Start ();
		rig = GetComponent<Rigidbody2D> ();//获取刚体组件
		InvokeRepeating("Shoot",1,shootlnterval);//开始重复调用方法（方法名,等待时间，间隔时间）
		audioSouce=gameObject.AddComponent<AudioSource>();
	}

	void Update()
	{	
		
		//获取输入
		float h  = Input.GetAxis("Horizontal");//AD
		float v = Input.GetAxis ("Vertical");//WS
		//构造一个移动向量
		Vector2 dir=new Vector2(h,v).normalized;
		//给刚体设置移动速度
		rig.velocity=dir*speed;

		//限制飞机上下不能飞出屏幕
		Vector2 pos= transform.position;
		pos.y = Mathf.Clamp (pos.y,-4.3f,4.57f);
		transform.position = pos;

		//右进左出
		if (h>0 && transform.position.x>3.2f) {

			Vector2 pos1=transform.position;
			pos1.x=-3.2f;
			transform.position = pos1;
		}

		//左进右出
		if (h<0 && transform.position.x<-3.2f) {
			Vector2 pos1 = transform.position;
			pos1.x = 3.2f;
			transform.position = pos1;
		}

	}

	//发射一颗子弹
	void Shoot()
	{
		//在飞机的位置生成一颗子弹
		Instantiate( bulletPrefab,transform.position,transform.rotation);
		audioSouce.PlayOneShot (shootsood);//播放音效
	}
}
